# with语句打开文件
# with open('test.txt','r') as reader:
#     content = reader.read()
#     print(content)

class FileManager(object):
    def __init__(self,name,mode):
        print("调用__init__方法")
        self.name = name
        self.mode = mode
        self.file = None

    # with语句执行时，会自动调用enter方法
    def __enter__(self):
        print("调用__enter__方法")
        self.file = open(self.name,self.mode)
        return self.file

    # 文件读取完毕后，会自动执行exit方法
    def __exit__(self, exc_type, exc_val, exc_tb):
        print("调用__exit__方法")
        if self.file:
            self.file.close()

with FileManager('test.txt','r') as file:
    print("准备读取文件")
    content = file.read()
    print(content)


